package cn.kgc.controller;

import cn.kgc.pojo.Orderid;
import cn.kgc.service.OrderidService;
import cn.kgc.util.StaticFinally;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Random;

@Controller
public class OrderidController {
    @Reference
    private OrderidService orderidService;

    @RequestMapping("/saveOrder.json")
    @ResponseBody
    private String  saveOrderid(Orderid orderid) {
        StringBuilder str = new StringBuilder();//定义变长字符串
        Random random = new Random();
        //随机生成数字，并添加到字符串
        for (int i = 0; i < 8; i++) {
            str.append(random.nextInt(10));
        }
        Integer flag = orderidService.getCount(str.toString());
        if (flag > 0) {
            saveOrderid(orderid);
        } else {
            orderid.setOrderNumber(str.toString());
            orderid.setPaystate(StaticFinally.PAYSTATE_NO);
            Integer count = orderidService.save(orderid);
            return "{\"orderNumber\":"+str.toString()+",\"result\":"+count+"}";
        }
        return "";
    }
}
